package com.xmmj.baselibrary.utils

import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

object Generic {

    fun <T> getSuperclassType(obj: Any, i: Int): T? {
        try {
            val aClass: Class<*> = obj.javaClass
            val genericSuperclass = aClass.genericSuperclass
            if (genericSuperclass is ParameterizedType) {
                val actualTypeArguments = genericSuperclass.actualTypeArguments
                val responseClass: Class<T> = actualTypeArguments[i] as Class<T>
                return responseClass.newInstance()
            }
            return null
        } catch (var7: InstantiationException) {
            var7.printStackTrace()
        } catch (var7: IllegalAccessException) {
            var7.printStackTrace()
        }
        return null
    }

    fun <T> getInterfaceType(obj: Any, i: Int): T? {
        try {
            val aClass: Class<*> = obj.javaClass
            val types = aClass.genericInterfaces
            val parameterizedType = types[0] as ParameterizedType
            val actualTypeArguments = parameterizedType.actualTypeArguments
            val responseClass: Class<T> = actualTypeArguments[i] as Class<T>
            return responseClass.newInstance()
        } catch (var7: InstantiationException) {
            var7.printStackTrace()
        } catch (var7: IllegalAccessException) {
            var7.printStackTrace()
        }
        return null
    }

    fun getSuperclassGenericType(obj: Any, i: Int): Type? {
        return try {
            val aClass: Class<*> = obj.javaClass
            val genericSuperclass = aClass.genericSuperclass
            if (genericSuperclass is ParameterizedType) {
                val actualTypeArguments = genericSuperclass.actualTypeArguments
                actualTypeArguments[i]
            } else {
                null
            }
        } catch (var6: Exception) {
            var6.printStackTrace()
            null
        }
    }

    fun getInterfaceGenericType(obj: Any, i: Int): Type? {
        return try {
            val aClass: Class<*> = obj.javaClass
            val types = aClass.genericInterfaces
            val parameterizedType = types[0] as ParameterizedType
            val actualTypeArguments = parameterizedType.actualTypeArguments
            actualTypeArguments[i]
        } catch (var6: Exception) {
            var6.printStackTrace()
            null
        }
    }

    fun forName(className: String?): Class<*>? {
        return try {
            Class.forName(className)
        } catch (var2: ClassNotFoundException) {
            var2.printStackTrace()
            null
        }
    }
}